package com.example.springboottestgit.controller.main;


import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: ZZZ
 */
//@Component
public class ExcelReader {
    int n = 0;

    public static void main(String[] args) throws IOException {
        String excelFilePath = "C:\\Users\\user\\Desktop\\beijing3200.csv";
        int columnIndex = 4; // 0-indexed column number
        List<String> list = readColumn(excelFilePath, columnIndex);
        System.out.println(list.toString());

    }

    public void init() throws IOException, InterruptedException {

        String excelFilePath = "C:\\Users\\user\\Desktop\\Mauville-MedCLIP\\MedCLIP\\data\\medpix.xlsx";
        int columnIndex = 4; // 0-indexed column number
        List<String> list = readColumn(excelFilePath, columnIndex);

        String directoryPath = "C:\\Users\\user\\Desktop\\Mauville-MedCLIP\\MedCLIP\\datav2\\output";
        File directory = new File(directoryPath);
//        if (directory.exists() && directory.isDirectory()) {
//            // 获取目录下所有文件名
//            String[] fileNames = directory.list();
//        }

        String[] fileNames = directory.list();
        List<String> fileNameList = new ArrayList<>(Arrays.asList(fileNames));

        for (String path : list) {
            if (path.length() > 30) {
                String fileName = path.substring(54);
                if (!fileNameList.contains(fileName)) {
                    String savePath = "C:\\Users\\user\\Desktop\\Mauville-MedCLIP\\MedCLIP\\datav2\\output2\\" + fileName;
                    System.out.println(savePath);
                    Thread.sleep(10);
                    downloadPicture(path, savePath);
                }

            }
        }
    }

    public static List<String> readColumn(String filePath, int columnIndex) {
        List<String> columnValues = new ArrayList<>();
        try (FileInputStream fileInputStream = new FileInputStream(new File(filePath));
             //xls HSSFWorkbook  xlsx XSSFWorkbook
             Workbook workbook = new XSSFWorkbook(fileInputStream)) {
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                Cell cell = row.getCell(columnIndex);
                if (cell != null) {
                    columnValues.add(cell.getStringCellValue());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return columnValues;
    }


    public void downloadPicture(String imageUrl, String savePath) throws IOException {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
            FileOutputStream out = new FileOutputStream(new File(savePath));

            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(buffer)) != -1) {
                out.write(buffer, 0, bytesRead);
            }

            // 下载完成，关闭输入输出流
            in.close();
            out.close();
            connection.disconnect();

            System.out.println("图片下载成功，保存路径：" + savePath);
            System.out.println(n++);
        } else {
            System.out.println("无法下载图片，响应码：" + responseCode);
        }
    }


}